<?php

namespace App\Http\Controllers\Backend\Order;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Orders\Order;
use Illuminate\Validation\Rule;

class OrderController extends Controller
{
    public function orderList(Request $request){
        //order status
        $order = new Order();
        $status = $order->status;
        return view('backend.order.orderlist')->with('status',json_encode($status));
    }
    public function search (Request $request){
        $order = new Order();
        $orders = $order->lists($request);
        //$orders = $order->list1($request);
        $count = $orders->count;

        $data = array('draw'=>$request->draw,'recordsTotal'=>$count,'recordsFiltered'=>$count,'data'=>$orders->list);
        // return view('frontend.welcome');
        return response()->json($data);
    }

    public function updateOrder(Request $request){
        $data['code'] = 0;
        $order = new Order();
        $this->validate($request, [
            $request->column => ['required',Rule::unique('orders')->ignore($request->id)]
        ],[
            $request->column.'.required' => '不能为空',
            $request->column.'.unique' => '已存在'
        ]);
        try{

            $order->updateRow($request);
            $val = '';
            if($request->column == 'status'){
                $val = $order->status[$request->input($request->column)];
            } else {
                $val = $request->input($request->column);
            }
            $data['data'] = $val;
        }catch (\Exception $e){
            $data['code'] = 500;
            $data['msg']  = $e->getMessage();
        }
        return response()->json($data);
    }
    public function delOrder(Request $request){
        $order = new order();

        $data['code'] = 0;

        try{
            $order->deleteRow($request);

        }catch (\Exception $e){
            $data['code'] = 403;//非法操作

            $data['msg'] = $e->getMessage();

        }

        return response()->json($data);
    }
    public function restoreOrder(Request $request){
        $order = new order();

        $data['code'] = 0;

        try{
            $order->activeRow($request);

        }catch (\Exception $e){
            $data['code'] = 403;//非法操作

            $data['msg'] = $e->getMessage();

        }

        return response()->json($data);
    }
    public function forceDelOrder(Request $request){
        $order = new order();

        $data['code'] = 0;

        try{
            $order->forceDelOrder($request);

        }catch (\Exception $e){
            $data['code'] = 403;//非法操作

            $data['msg'] = $e->getMessage();

        }

        return response()->json($data);
    }
}
